// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { NotificationCenter } from "../utils/NotificationCenter";
import { S2CProtocol } from "../proto/ELProtocol";

const {ccclass, property} = cc._decorator;
const actionStr = ['兑换','分红枪分红','战队转盘','战队抽奖次数必送奖励','提现'];
@ccclass
export default class NewClass extends cc.Component {

    private recordData: Array<rmbRecordInfo>;
    private total: number;

    @property(cc.Prefab)
    recordItem: cc.Prefab = null;

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        NotificationCenter.Instance.on(S2CProtocol.s2c_rmb_list, this.addRMBList, this);
    }
    
    onDestroy() {
        NotificationCenter.Instance.off(S2CProtocol.s2c_rmb_list, this.addRMBList, this);
        // let resList = cc.loader.getDependsRecursively('prefab/rmb_list_layer');
        // var textures = [];
		// for (var i = 0; i < resList.length; ++i) {
		//     var item = cc.loader.getRes(resList[i]);
		//     if (item instanceof cc.Texture2D) {
		//         textures.push(item);
		//     }
        // }
    }

    start () {
        this.recordData = new Array<rmbRecordInfo>();
        this.total = 0;

        let scroll_list = cc.find('im_bg/scroll_list', this.node);
        let scorll_content = cc.find('view/content', scroll_list);
        scroll_list.getComponent(cc.ScrollView).scrollToTop(0.01);
        scroll_list.on('scroll-to-bottom', this.bottomUpdate, this);
        scorll_content.removeAllChildren();

        NotificationCenter.Instance.emit("requestRMBHistory", 1);
    }

    // update (dt) {}

    public clickClose() {
        this.node.active = false;
        this.node.destroy();
    }

    private addRMBList(data: any) {
        if (data.total == 0) {
            return;
        }
        this.total = data.total;
        if (data.total <= this.recordData.length) {
            return;
        }

        this.recordData = this.recordData.concat(data.data);

        let scroll_list = cc.find('im_bg/scroll_list', this.node);
        let scorll_content = cc.find('view/content', scroll_list);

        let itemHeight = 0;
        for (let i = 0; i < data.data.length; i++) {
            const fdata = data.data[i];
            let item = cc.instantiate(this.recordItem);
            itemHeight = item.getContentSize().height;
            let item_bg = item.getChildByName('im_bg');
            item_bg.active = (i%2 == 1);
            let item_name = item.getChildByName('label_name');
            item_name.getComponent(cc.Label).string = actionStr[fdata.action-320];
            let label_time = item.getChildByName('label_time');
            let date = new Date(fdata.createTime);
            let dateStr = date.getFullYear()+'-'+
                            (date.getMonth()+1)+'-'+
                            date.getDate()+'  '+
                            date.getHours()+":"+
                            date.getMinutes()+":"+
                            date.getSeconds();
            // let dateStr = date.toDateString()+'  '+date.getHours()+":"+
            //                 date.getMinutes()+":"+
            //                 date.getSeconds();
            label_time.getComponent(cc.Label).string = dateStr;

            let label_amount = item.getChildByName('label_amount');
            label_amount.getComponent(cc.Label).string = "￥    "+(fdata.addrmb>=0?'+':'')+fdata.addrmb/100;
            label_amount.color = (fdata.addrmb>=0)?(cc.Color.BLACK.fromHEX("#16D91F")):(cc.Color.BLACK.fromHEX("#D61818"));

            item.y = -itemHeight*i;
            item.parent = scorll_content;
        }

        scorll_content.height = this.recordData.length*itemHeight;
    }

    public bottomUpdate() {
        cc.log('bottomUpdate1:'+this.total+'/'+this.recordData.length);
        if (this.total <= this.recordData.length) {
            return;
        }

        let page = Math.ceil(this.recordData.length/10)+1;

        cc.log('bottomUpdate3:'+page);
        NotificationCenter.Instance.emit("requestRMBHistory", page);
    }
}
